import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";
import LvItem from "../../LvItem";
import LvItem_15 from "./LvItem_15";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import LevelPre_15 from "./LevelPre_15";
import { UIManager } from "../../manager/UIManager";
import { AudioManager1 } from "../../manager/AudioManager1";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_258 extends cc.Component {
    @property(sp.Skeleton)
    nv: sp.Skeleton = null;
    level15: LevelPre_15 = null;
    Start_sh:boolean = false;
    changxi:boolean = false;
    zhijieshibai:boolean = false;
    lastNode: any = null;
    t = 0
    m = 0
    start(): void {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);

        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);

    }
    chang() {
        if(!this.zhijieshibai)
        {
            console.warn("???????")
            this.changxi = true;
            AudioManager1.playAudio("唱戏bgm", 0.5, BUNDLE_TYPE_ENUM.LEVEL);
            this.scheduleOnce(()=>{
                this.buchang();
            },26) 
            

        }
       
    }
    buchang()
    {         
        if(!this.zhijieshibai)
        {
            console.warn("!!!!!!!!")
            this.changxi = false;
            AudioManager1.stopAudioForName("唱戏bgm");
            this.scheduleOnce(()=>{
                this.chang();
            },5)  
        }           
        
    }
    
    gN(NodeName: string) {
        if (!this.level15.getNodeForName(NodeName).getComponent(LvItem)) {
            this.level15.getNodeForName(NodeName).addComponent(LvItem)
        }
        return this.level15.getNodeForName(NodeName);
    }
    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }
    async finishTween(finishStr: string, lvItem: LvItem) {
        if (typeof finishStr != "string") {
            return;
        }
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        // if (finishStr === "reviveGame") {
        //     this.chang();
        //     this.nv.setAnimation(0, "dou", true);
        //     this.gN("药").active = true;
        //     UIManager.ins.enabledGameClick(true);
        //     // AudioManager.playBG("恐怖背景音乐", 0.5, BUNDLE_TYPE_ENUM.LEVEL)
        //     this.gN("shibai").active = false;
        //     this.level15.removeStates(["7"])
        //     if(this.level15.isHaveStates(["-4"]))
        //     {
        //         this.level15.removeStates(["-4"])
        //         this.gN("隔音棉").active = true;
        //         this.gN("qiang").active = true;
        //         this.gN("隔音板缺").active = false;
        //     }
        //     if(this.level15.isHaveStates(["-5"]))
        //     {
        //         this.level15.removeStates(["-5"])
        //         this.gN("隔音条").active = true;
        //         this.gN("chuangzi").active = true;
        //         this.gN("胶带没摸胶水").active = false;
        //     }
        // }
        if (finishStr.includes("Start说话")) {
            
            if(!this.Start_sh)
            {
                UIManager.ins.enabledGameClick(false);
                this.Start_sh = true;
                this.nv.setAnimation(0, "daiji_sh", true);
                this.level15.sayNodeQipaoTween({ node: "nvSayNode", str: "不能听她的歌声吗！赶紧看看周围有没有可以隔绝的办法！", audio: { resUrl: "不能听她的歌声吗！赶紧看看周围有没有可以隔绝的办法！", value: 1 } });
                this.scheduleOnce(()=>{
                    this.chang();
                    this.nv.setAnimation(0, "dou", true);
                    this.gN("huadan").getComponent(sp.Skeleton).setAnimation(0, "zou", true)
                    UIManager.ins.enabledGameClick(true);
                },4.8)
                
            }else
            {
                UIManager.ins.enabledGameClick(true);
            }
        }
        if (finishStr.includes("WinOrOver")) {
            if(this.level15.isHaveStates(["1","2","3","4","5","6","7","墙胶水","窗胶水"]))
            {
                if(!this.changxi)
                {
                    cc.Tween.stopAllByTarget(this.lastNode);
                    AudioManager1.stopAllAudio();
                    this.zhijieshibai = true;
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni1").interTween);
                }else
                {
                    AudioManager1.stopAllAudio();
                    this.zhijieshibai = true;
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("winGameAni").interTween);  
                }
               
            }else if(this.level15.isHaveNoStates(["1"])||
                    this.level15.isHaveNoStates(["2"])||
                    this.level15.isHaveNoStates(["3"])||
                    this.level15.isHaveNoStates(["4"])||
                    this.level15.isHaveNoStates(["5"])||
                    this.level15.isHaveNoStates(["墙胶水"])||
                    this.level15.isHaveNoStates(["窗胶水"]))
                    {
                        AudioManager1.stopAllAudio();
                        cc.Tween.stopAllByTarget(this.lastNode);
                        this.zhijieshibai = true;
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni3").interTween);
                    }else if(this.level15.isHaveNoStates(["6"]))
                    {
                        this.zhijieshibai = true;
                        AudioManager1.stopAllAudio();
                        cc.Tween.stopAllByTarget(this.lastNode);
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                    }
            
        }
        if (finishStr.includes("lastNode")) {
            this.lastNode = lvItem.node;
        }
        if (finishStr.includes("唱戏停否")) {
            if(!this.changxi)
            {
                cc.Tween.stopAllByTarget(this.lastNode);
                 AudioManager1.stopAllAudio();
                 this.zhijieshibai = true;
                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni1").interTween);
            }else
            {
                
            }
        }
    }
    protected update(dt: number): void {
        switch(this.t)
        {
            case 0:
                this.gN("huadan").x++
                if(this.gN("huadan").x>=420)
                {
                    this.t = 1;
                }
                break;
            case 1:
                this.gN("huadan").x--
                if(this.gN("huadan").x<=80)
                {
                    this.t = 0;
                }
                break;
        }
    }

}
